﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using StackExchange.Redis;

namespace RedisLib
{
    public partial class RedisClient
    {
        public async Task AAA(string script, object parameters = null, int db = -1, CancellationToken cancellationToken = default)
        {
            IDatabase database = await GetConnectedDatabase(db, cancellationToken);
            if (database == null) return;


            LuaScript luaScript = LuaScript.Prepare(script);
            await database.ScriptEvaluateAsync(luaScript, parameters);
        }

        public async Task LuaDeleteKeyByPattern(string pattern, int db = -1, CancellationToken cancellationToken = default)
        {
            IDatabase database = await GetConnectedDatabase(db, cancellationToken);
            if (database == null) return;

            var mask = pattern;
            string luaScript = "local keys = redis.call('keys', ARGV[1]) for i=1,#keys,5000 do redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) end return keys";
            var result = await database.ScriptEvaluateAsync(luaScript, null, [mask]);
        }
    }
}

// http://redisdoc.com/script/index.html
// https://redis.io/docs/manual/programmability/lua-debugging/

// redis中如何使用lua脚本让你的灵活性提高5个逼格详解
// https://www.jb51.net/article/148830.htm
// http://www.lua.org/

// 脚本注释多行_Redis Lua脚本中学教程
// https://blog.csdn.net/weixin_42352981/article/details/112582627

// Redis Lua脚本中学教程（上）
// https://juejin.cn/post/6844903869974052878
// Redis Lua脚本中学教程（下）
// https://juejin.cn/post/6844903872071204872
// Redis Lua脚本大学教程
// https://juejin.cn/post/6844903873870561293
